//
//  AppDelegate.swift
//  02- 新浪微博(swift实现)
//
//  Created by 刘 伟生 on 16/1/7.
//  Copyright © 2016年 seasa. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
         print(UserCount.loadUserCountFromFile())
    
      
        
        //设置tabBar上的主题颜色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        //设置navigationBar上的颜色
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
                
//        //初始化window
        let rect : CGRect = UIScreen.mainScreen().bounds
        window = UIWindow(frame: rect);
//
        //设置window的跟控制器
        //创建一个跟控制器
        window?.rootViewController = defoultViewController()
        window?.makeKeyAndVisible()

        
        
        //注册通知监听
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("switchRootViewController:"), name: XMGChangeRootViewControllerNotification, object: nil)
        
        
        return true
    }

}

extension AppDelegate {
    
    func defoultViewController() -> UIViewController {
        
        var storyBoard  = UIStoryboard(name: "Main", bundle: nil)
        //判断是否登录 否的话就启动main.storyBoard,是的话就判断是否更新版本
        if UserCount.islogin() == false {
            
            
            
            //判断是否更新版本,是的话就启动新特性页面,否的话就进入欢迎界面
        } else if isNewVersion() {
            
            storyBoard = UIStoryboard(name: "NewFeature", bundle: nil)
            
        }else{
            
            storyBoard = UIStoryboard(name: "WelComesView", bundle: nil)
            
        }
        
        let startVC = storyBoard.instantiateInitialViewController()
        
        return startVC!
    }
    
    private func isNewVersion() -> Bool {
        
        guard let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as? String else {
            return false
        }
        //获取偏好设置
        let defoult = NSUserDefaults.standardUserDefaults()
        
        //从偏好设置中获取版本号
        let proviousVersion = (defoult.objectForKey("proviosVersionKey") as? String) ?? "0"
        
        //比较两个版本号是否相同
        if currentVersion.compare(proviousVersion) == NSComparisonResult.OrderedDescending {
            
            //能进来说明有是新版本,那么除了需要返回true外,还需要将新的版本号偏好设置保存到本地中,方便下次获取
            defoult.setObject(currentVersion, forKey: "proviosVersionKey")
            return true
        }
        //没有更新版本
        return false
    }
    
    
    //实现监听通知的方法
    @objc private func switchRootViewController(notification : NSNotification) {
        
        //首先取出通知传递过来的通知信息,里面包含一个字典
        guard let info = notification.userInfo else {
            return
        }
        
        guard let message = info["message"] as? Bool else {
            return
        }
        
        
        
        //显示界面跳转 
        var storyBoard  = UIStoryboard(name: "Welcome", bundle: nil)
        if message {
            storyBoard  = UIStoryboard(name: "Main", bundle: nil)
        }
        
        let rootVC =  storyBoard.instantiateInitialViewController()
        window?.rootViewController = rootVC
    }
    
}

